# -*- coding: utf-8 -*-
"""
    账号管理
"""
from flask import Blueprint,request,jsonify
from application import app,db
import requests,json
from common.models.member.Member import Member
from common.models.member.OauthMemberBind import OauthMemberBind
from common.libs.Helper import get_current_date
from common.libs.member.MemberService import MemberService


route_api = Blueprint( 'api_page',__name__ )

@route_api.route( "/login", methods = ['GET','POST'] )
def login():
    resp = {
        'code': 200,
        'msg': '登录成功',
        'data': {}
    }

    req = request.values # 前端请求的参数

    code = req['code'] if 'code' in req else '' # 获取用户code

    if not code or len(code) < 1:
        resp['code'] = -1
        resp['msg'] = '需要code'
        return jsonify(resp)

    open_id = MemberService.getWeChatOpenId(code) # 获取openid
    if open_id is None:
        resp['code'] = -1
        resp['msg'] = '获取openid失败'
        return jsonify(resp)

    nickname = req['nickName'] if 'nickName' in req else ''
    gender = req['gender'] if 'gender' in req else ''
    avatar = req['avatarUrl'] if 'avatarUrl' in req else ''

    # 判断是否已经注册过
    bind_info = OauthMemberBind.query.filter_by(openid = open_id, type=1).first()

    # 已经绑定过
    if bind_info:
        member_info = Member.query.filter_by(id = bind_info.member_id).first()
        token = "%s#%s" % (MemberService.geneAuthCode(member_info), member_info.id)
        resp['code'] = 200
        resp['msg'] = '已经绑定'
        resp['data'] = {
            'token': token
        }
        return jsonify(resp)

    # 未绑定过，该用户为新用户
    model_member = Member()
    model_member.nickname = nickname # 用户昵称
    model_member.gender = gender # 用户性别 1男性 2女性
    model_member.avatar = avatar # 用户头像
    model_member.salt = MemberService.geneSalt() # 加密字段
    model_member.created_time = get_current_date() # 创建时间
    model_member.updated_time = get_current_date() # 更新时间

    db.session.add(model_member)
    db.session.commit()

    model_bind = OauthMemberBind()
    model_bind.member_id = model_member.id # 用户id
    model_bind.type = 1
    model_bind.openid = open_id
    model_bind.extra = ''
    model_bind.created_time = get_current_date()  # 创建时间
    model_bind.updated_time = get_current_date()  # 更新时间

    db.session.add(model_bind)
    db.session.commit()

    bind_info = model_bind

    member_info = Member.query.filter_by(id=bind_info.member_id).first()  # 获取用户信息
    token = "%s#%s" % (MemberService.geneAuthCode(member_info), member_info.id)

    resp['data'] = {
        'token': token
    }

    return jsonify(resp)



@route_api.route( "/check-reg", methods = ['GET','POST'] )
def checkReg():
    resp = {
        'code': 200,
        'msg': '登录成功',
        'data': {}
    }

    req = request.values  # 前端请求的参数

    code = req['code'] if 'code' in req else ''  # 获取用户code

    if not code or len(code) < 1:
        resp['code'] = -1
        resp['msg'] = '需要code'
        return jsonify(resp)

    open_id = MemberService.getWeChatOpenId(code)  # 获取openid
    if open_id is None:
        resp['code'] = -1
        resp['msg'] = '获取openid失败'
        return jsonify(resp)

    # 判断是否已经注册过
    bind_info = OauthMemberBind.query.filter_by(openid=open_id, type=1).first()
    if not bind_info:
        resp['code'] = -1
        resp['msg'] = '未绑定该用户'
        return jsonify(resp)

    member_info = Member.query.filter_by(id=bind_info.member_id).first() # 获取用户信息
    if not member_info:
        resp['code'] = -1
        resp['msg'] = '未查询到绑定信息'
        return jsonify(resp)

    token = "%s#%s"%( MemberService.geneAuthCode(member_info), member_info.id )

    resp['data'] = {
        'token': token
    }

    return jsonify(resp)
